<template>
  <div class="employees">
    <el-card class="card">
      <!-- 通过自定义指令控制按钮的显示隐藏
      原理:就是判断权限管理对应属性是否包含该值以控制显示隐藏
       -->
      <el-button
        type="primary"
        size="small"
        @click="importUser"
        v-control="'employees_import_button'"
        >导入</el-button
      >
      <el-button type="primary" size="small" @click="add">+ 新增员工</el-button>
      <el-button v-print="{ id: 'card' }">打印</el-button>
    </el-card>
    <el-card id="card">
      <el-table :data="list" border>
        <el-table-column label="序号" prop="">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="头像" prop="staffPhoto">
          <template v-slot="{ row }">
            <img
              :src="row.staffPhoto"
              alt=""
              class="avatar"
              v-imgerror="errorImg"
              @click="imgClick(row.staffPhoto)"
            />
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile"></el-table-column>
        <el-table-column label="工号" prop="workNumber"></el-table-column>
        <el-table-column label="聘用形势" prop="formOfEmployment">
          <!-- 表格自带方法
          :formatter="formatter" -->
          <!-- 过滤器 
        <template v-slot="{row}">
          <div>
            {{row.formOfEmployment | formatType}}
          </div>
        </template>
        -->
        </el-table-column>
        <el-table-column label="部门" prop="departmentName"></el-table-column>
        <el-table-column label="入职时间" prop="correctionTime">
          <template v-slot="{ row }">
            <div>
              {{ row.correctionTime | formatTime }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="enableState"></el-table-column>
        <el-table-column label="操作" width="300px" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="goDetails(row.id)">查看</el-button>
              <el-button type="text" @click="positive(row.id)">转正</el-button>
              <el-button type="text" @click="adjustPost(row.id)"
                >调岗</el-button
              >
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="role(row.id)">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-sizes="[1, 5, 10, 20, 100, 200]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <DialogItem :isShow.sync="show" @getData="getData"></DialogItem>
    <!-- 转正 -->
    <PositiveDialog ref="positiveDialog"></PositiveDialog>
    <!-- 二维码弹窗 -->
    <el-dialog :visible.sync="showCanvas" title="二维码">
      <div style="text-align:center"><canvas ref="canvas"></canvas></div>
    </el-dialog>
    <!-- 角色弹窗 -->
    <DialogRole ref="dialogRole"></DialogRole>
  </div>
</template>

<script>
import { sysUser, sysUserDel } from '@/api/employees'
import DialogItem from './components/dialogItem.vue'
import employees from '@/assets/constant/employees'
import PositiveDialog from './components/positiveDialog.vue'
import DialogRole from './components/dialogRole.vue'
import Qrcode from 'qrcode'
export default {
  components: {
    DialogItem,
    PositiveDialog,
    DialogRole
  },
  data () {
    return {
      showCanvas: false,
      errorImg: require('@/assets/common/head.jpg'),
      show: false,
      list: [{}],
      page: {
        page: 1,
        size: 10
      },
      total: 0
    }
  },
  created () {
    this.getData()
  },
  methods: {
    // 获取数据
    async getData () {
      const res = await sysUser(this.page)
      this.list = res.data.rows
      // 原始数据转换
      res.data.rows.forEach(item => {
        item.formOfEmployment = this.formatter(
          null,
          null,
          +item.formOfEmployment
        )
      })
      this.total = res.data.total
      console.log(res)
    },
    // 页容量改变
    handleSizeChange (size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    // 页码改变
    handleCurrentChange (page) {
      this.page.page = page
      this.getData()
    },
    // el-table自带方法  接收四个参数
    formatter (row, column, cellValue, index) {
      const result = employees.hireType.find(item => item.id === cellValue)
      return result ? result.value : '-'
    },
    // 新增员工
    add () {
      this.show = true
    },
    // 删除员工
    del (row) {
      this.$confirm('确定要删除该员工吗?', '提示')
        .then(async () => {
          await sysUserDel(row.id)
          this.$message.success('删除成功')
          this.handleCurrentChange(1)
        })
        .catch(() => {})
    },
    // 批量导入
    importUser () {
      this.$router.push(
        '/employees/import?name=' +
          encodeURIComponent('员工') +
          `&redirect=${encodeURIComponent('/employees')}`
      )
    },
    // 查看修改
    goDetails (id) {
      this.$router.push('/employees/details/' + id)
    },
    // 转正
    positive (id) {
      this.$refs.positiveDialog.isShow = true
      this.$refs.positiveDialog.form.userId = id
    },
    // 点击头像生成二维码
    imgClick (url) {
      if (!url) {
        this.$message.error('没有图片')
      } else {
        this.showCanvas = true
        this.$nextTick(() => {
          Qrcode.toCanvas(this.$refs.canvas, url, { width: 200, height: 200 })
        })
      }
    },
    // 调岗
    adjustPost (id) {
      this.$router.push('/employees/adjustPost/' + id)
    },
    // 角色
    role (id) {
      this.$refs.dialogRole.showRole = true
      this.$refs.dialogRole.getUserInfo(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  .card {
    text-align: right;
    margin-bottom: 15px;
  }
  .pagination {
    text-align: right;
    padding-top: 5px;
  }
  .avatar {
    width: 40px;
    height: 40px;
    object-fit: contain;
  }
}
</style>
